package com.sw.test01.ui.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.sw.test01.R;

public class MainActivity extends BaseActivity implements View.OnClickListener {
    private Button btn1;
    private Button btn2;
    private Button btnTalk;
    private Button btnNews;
    private Button btnBroadcast;
    private Button btnRTPermission;
    private Button btnContentProvidar;
    private Button btnNT;
    private Button btnNT2;
    private Button btnNotificationIn;
    private Button btnCameraIn;
    private Button btnWebView;
    private Button btnXml;
    private Button btnJson;
    private Button btnFrontService;
    private NetworkChangedReceiver receiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        listener();

        IntentFilter intentFilter = new IntentFilter("android.net.ocnn.CONNECTIVITY_CHAGNE");
        receiver = new NetworkChangedReceiver();
        registerReceiver(receiver,intentFilter);
    }

    private void initView() {
        btn1 = (Button) findViewById(R.id.btn01);
        btn2 = (Button) findViewById(R.id.btn2);
        btnTalk = (Button) findViewById(R.id.btn_talk);
        btnNews = (Button) findViewById(R.id.btn_news);
        btnBroadcast = (Button) findViewById(R.id.btn_broadcast);
        btnRTPermission = (Button) findViewById(R.id.btn_rt_permission);
        btnNT = (Button) findViewById(R.id.btn_navigation_toolbar);
        btnNT2 = (Button) findViewById(R.id.btn_navigation_toolbar2);
        btnContentProvidar = (Button) findViewById(R.id.btn_content_providar);
        btnNotificationIn = (Button) findViewById(R.id.btn_notification_in);
        btnCameraIn = (Button) findViewById(R.id.btn_camera_in);
        btnWebView = (Button) findViewById(R.id.btn_webview_in);
        btnXml = (Button) findViewById(R.id.btn_xml);
        btnJson = (Button) findViewById(R.id.btn_json);
        btnFrontService = (Button) findViewById(R.id.btn_front_service);
    }

    private void listener() {
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btnTalk.setOnClickListener(this);
        btnNews.setOnClickListener(this);
        btnBroadcast.setOnClickListener(this);
        btnRTPermission.setOnClickListener(this);
        btnNT.setOnClickListener(this);
        btnNT2.setOnClickListener(this);
        btnContentProvidar.setOnClickListener(this);
        btnNotificationIn.setOnClickListener(this);
        btnCameraIn.setOnClickListener(this);
        btnWebView.setOnClickListener(this);
        btnXml.setOnClickListener(this);
        btnJson.setOnClickListener(this);
        btnFrontService.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn01:
                Intent intent = new Intent("com.sw.test01.ACTION_START");
                startActivity(intent);
                break;
            case R.id.btn2:
                Intent intent1 = new Intent(MainActivity.this,ThirdActivity.class);
                startActivity(intent1);
                break;
            case R.id.btn_talk:
                Intent intent2 = new Intent(MainActivity.this,TalkActivity.class);
                startActivity(intent2);
                break;
            case R.id.btn_news:
                Intent intent3 = new Intent(MainActivity.this,FragmentActivity.class);
                startActivity(intent3);
                break;
            case R.id.btn_broadcast:
                Intent intent4 = new Intent(MainActivity.this,LoginActivity.class);
                startActivity(intent4);
                break;
            case R.id.btn_rt_permission:
                Intent intent5 = new Intent(MainActivity.this,RTPActivity.class);
                startActivity(intent5);
                break;
            case R.id.btn_navigation_toolbar:
                Intent intent6 = new Intent(MainActivity.this, Main2Activity.class);
                startActivity(intent6);
                break;
            case R.id.btn_navigation_toolbar2:
                Intent intent7 = new Intent(MainActivity.this, NTActivity.class);
                startActivity(intent7);
                break;
            case R.id.btn_content_providar:
                Intent intent8 = new Intent(MainActivity.this, ContentProvidarActivity.class);
                startActivity(intent8);
                break;
            case R.id.btn_notification_in:
                Intent intent9 = new Intent(MainActivity.this, NotificationActivity.class);
                startActivity(intent9);
                break;
            case R.id.btn_camera_in:
                Intent intent10 = new Intent(MainActivity.this, CameraActivity.class);
                startActivity(intent10);
                break;
            case R.id.btn_webview_in:
                Intent intent11 = new Intent(MainActivity.this, WebViewActivity.class);
                startActivity(intent11);
                break;
            case R.id.btn_xml:
                Intent intent12 = new Intent(MainActivity.this, XmlActivity.class);
                startActivity(intent12);
                break;
            case R.id.btn_json:
                Intent intent13 = new Intent(MainActivity.this, JsonActivity.class);
                startActivity(intent13);
                break;
            case R.id.btn_front_service:
                Intent intent14 = new Intent(MainActivity.this, ServerActivity.class);
                startActivity(intent14);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }

    private class NetworkChangedReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
            if (networkInfo != null && networkInfo.isAvailable()){
                Toast.makeText(MainActivity.this,"network is available",Toast.LENGTH_SHORT);
            }else {
                Toast.makeText(MainActivity.this,"network is unavaliable",Toast.LENGTH_SHORT);
            }
        }
    }
}
